package fule.com.playandroidkotlin.ui.activity

import android.content.Intent
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import fule.com.playandroidkotlin.R
import fule.com.playandroidkotlin.base.BaseActivity
import fule.com.playandroidkotlin.ui.adapter.SearchResultAdapter
import fule.com.playandroidkotlin.ui.model.SearchModel
import fule.com.playandroidkotlin.ui.presenter.SearchResultPresenter
import fule.com.playandroidkotlin.ui.util.AppConstant
import fule.com.playandroidkotlin.ui.view.SearchResultView
import fule.com.playandroidkotlin.ui.weight.DividerDecoration
import kotlinx.android.synthetic.main.activity_search_result.*
import kotlinx.android.synthetic.main.toolbar.*


/**
 *@author: njb
 *@date:   2020/2/6 0006 18:09
 *@desc:
 */
class SearchResultActivity : BaseActivity<SearchResultPresenter>(), SearchResultView {
    private var keyword: String? = null
    private var searchAdapter: SearchResultAdapter? = null
    private var page:Int = 0

    override val layoutId: Int
        get() = R.layout.activity_search_result

    override fun createPresenter(): SearchResultPresenter {
        return SearchResultPresenter(this);
    }

    override fun initView() {
        initIntent()
        initAdapter()
        toolbar_title.text = "搜索"
        iv_search.visibility = View.GONE
        toolbar.setNavigationOnClickListener { finish() }
    }

    private fun initAdapter() {
        searchAdapter = SearchResultAdapter(null)
        searchAdapter.run {
            var manager = LinearLayoutManager(context)
            rv_result.layoutManager = manager
            searchAdapter!!.bindToRecyclerView(rv_result)
        }
        rv_result.run {
            rv_result.addItemDecoration(DividerDecoration(ContextCompat.getColor(context,R.color.ce7e7e7), 1))
            rv_result.adapter = searchAdapter
        }
    }

    private fun initIntent() {
        if (intent != null && intent.extras != null) {
            intent.extras.let {
                keyword = intent.extras!!.getString(AppConstant.ARTICLELIST_TITLE)
                presenter!!.query(page, keyword ?: "")
            }
        }
    }

    override fun addListener() {
        searchAdapter!!.setOnItemClickListener { _, _, position ->
            Intent(context, WebViewActivity::class.java).run {
                putExtra(AppConstant.WEBVIEW_ID_KEY, searchAdapter!!.data[position].id)
                putExtra(AppConstant.WEBVIEW_TITLE_KEY, searchAdapter!!.data[position].title)
                putExtra(AppConstant.WEBVIEW_URL_KEY, searchAdapter!!.data[position].link)
                startActivity(this, null)
            }
        }

        rf_result.setOnRefreshListener{
            page = 0
            presenter!!.query(page, keyword ?: "")
        }
        rf_result.setOnLoadMoreListener {
            page++
            presenter!!.query(page, keyword ?: "")
        }
    }

    override fun onLoadSearcher(data: SearchModel) {
        if(page == 0){
            if(data.datas!!.size == 0){
                searchAdapter!!.setEmptyView(R.layout.empty_layout_no_data)
            }
            searchAdapter!!.setNewData(data.datas)
        }else{
            data.datas?.let { searchAdapter!!.addData(it) }
        }
        rf_result.finishRefresh(300)
        rf_result.finishLoadMore(300)

    }

    override fun showLoading() {
        showWaitDialog()
    }

    override fun hideLoading() {
        dismissWaitDialog()
    }
}